package slavik.voloshyn.vashmagazyn;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class ParsingFile extends Thread
{

	private final Object monitor;
	private final List<Announcement> listAnnouncement = new ArrayList<Announcement>();
	private final CustomMap<CustomVector<String>, Announcement> mapAnnouncement = new CustomMap<CustomVector<String>, Announcement>();
	private int runnigIndex = 0;
	private String textFromFile;
	private final JTextArea textAreaResult;
	private final JLabel labelProgress;

	protected ParsingFile(Object theMonitor, JTextArea theTextAreaResult,
	        JLabel theLabelProgress)
	{
		monitor = theMonitor;
		textAreaResult = theTextAreaResult;
		labelProgress = theLabelProgress;
	}

	@Override
	public void run()
	{
		super.run();
		synchronized (monitor)
		{
			try
			{
				monitor.wait();
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}

		System.out.println("start ParsingFile");

		textFromFile = readAllFile().toString();
		while (true)
		{
			runnigIndex = textFromFile.indexOf(Const.TAG_START_STREET,
			        runnigIndex);
			if (runnigIndex < 0)
			{
				break;
			}

			String street = searchTAG(Const.TAG_START_STREET,
			        Const.TAG_END_STREET);

			String countRoom = searchTAG(Const.TAG_START_COUNT_ROOM,
			        Const.TAG_END_COUNT_ROOM);

			String floor = searchTAG(Const.TAG_START_FLOOR, Const.TAG_END_FLOOR);

			String area = searchTAG(Const.TAG_START_AREA, Const.TAG_END_AREA);

			String price = searchTAG(Const.TAG_START_PRICE,
			        Const.TAG_END_PRICE, Const.GARBAGE_IN_PRICE_TAG);

			String currency = searchTAG(Const.TAG_START_CURRENCY,
			        Const.TAG_END_CURRENCY);

			String phone = searchTAG(Const.TAG_START_PHONE,
			        Const.TAG_END_PHONE, Const.GARBAGE1_IN_PHONE_TAG,
			        Const.GARBAGE2_IN_PHONE_TAG);
			String description = searchTAG(Const.TAG_START_DESCRIPTION,
			        Const.TAG_END_DESCRIPTION);

			if (phone == null || phone.contentEquals(""))
			{
				continue;
			}
			boolean state = false;
			for (int i = 0; i < phone.length(); i++)
			{
				if (Character.isDigit(phone.charAt(i)))
				{
					state = true;
					break;
				}
			}
			if (!state)
			{
				continue;
			}

			final Announcement announcement = new Announcement(countRoom,
			        phone, price, description, street, floor, area, currency);

			listAnnouncement.add(announcement);
			mapAnnouncement.put(announcement.getKeys(), announcement);
			System.out.println(announcement);
			runnigIndex++;

		}

		// textAreaResult.setText("123123123");
		// SwingUtilities.invokeLater(this);
		SwingUtilities.invokeLater(new Runnable()
		{
			@Override
			public void run()
			{
				for (Iterator<CustomVector<String>> iterator = mapAnnouncement
				        .keySet().iterator(); iterator.hasNext();)
				{
					textAreaResult.setText(textAreaResult.getText()
					        + mapAnnouncement.get(iterator.next()).toString()
					        + Const.SEPARATE_BETWEEN_ANNOUCEMENT);
				}
			}
		});

		SwingUtilities.invokeLater(new Runnable()
		{
			@Override
			public void run()
			{
				labelProgress.setVisible(false);
			}
		});
	}

	private String searchTAG(String theTagStart, String theEndTag,
	        String... theGarbage)
	{
		String textTag = searchTAG(theTagStart, theEndTag);
		for (int i = 0; i < theGarbage.length; i++)
			while (true)
			{
				if (textTag.indexOf(theGarbage[i]) >= 0)
				{
					String temp1 = textTag.substring(0,
					        textTag.indexOf(theGarbage[i]));
					String temp2 = textTag.substring(
					        textTag.indexOf(theGarbage[i])
					                + theGarbage[i].length(), textTag.length());
					textTag = temp1 + temp2;
				}
				else
				{
					break;
				}
			}
		return textTag;
	}

	private String searchTAG(String theTagStart, String theEndTag)
	{
		runnigIndex = textFromFile.indexOf(theTagStart, runnigIndex)
		        + theTagStart.length();
		String textTag = textFromFile.substring(runnigIndex,
		        textFromFile.indexOf(theEndTag, runnigIndex));
		return textTag;
	}

	private StringBuffer readAllFile()
	{
		StringBuffer fileBuffer = new StringBuffer();
		try
		{

			FileInputStream fstream = new FileInputStream(Const.PATH);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null)
			{
				fileBuffer.append(strLine);
			}
			in.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return fileBuffer;
	}
}
